use crate::error::*;
use crate::messages::*;
use crate::transporter::*;
use ascii::AsciiString;
use himmelblau_kerberos_asn1::{AsRep, Asn1Object, KrbError};
use himmelblau_kerberos_crypto::Key;
use std::collections::HashSet;
use std::net::IpAddr;

/// Encapsule the possible responses to an AS-REQ request
#[derive(Debug, PartialEq)]
pub enum AsReqResponse {
    KrbError(KrbError),
    AsRep(AsRep),
}

/// Send the AS-REQ requests and retrieves the response
///
/// # Examples
///
/// ```no_run
/// use himmelblau_kerbeiros::*;
/// use ascii::AsciiString;
/// use std::net::*;
/// use himmelblau_kerberos_constants::etypes::AES256_CTS_HMAC_SHA1_96;
/// use himmelblau_kerberos_crypto::Key;
///
/// // Prepare the arguments
/// let realm = AsciiString::from_ascii("CONTOSO.COM").unwrap();
/// let kdc_address = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1));
/// let username = AsciiString::from_ascii("Bob").unwrap();
/// let user_key = Key::Secret("S3cr3t".to_string());
///
/// let mut as_requester = himmelblau_kerbeiros::AsRequester::new(realm, kdc_address);
///
/// // Use AES-256 cipher
/// as_requester.set_etype(AES256_CTS_HMAC_SHA1_96).unwrap();
///
/// let response = as_requester.request(&username, Some(&user_key)).unwrap();
///
/// match response {
///     AsReqResponse::KrbError(krb_error) => {
///         println!("KRB-ERROR with error code = {}", krb_error.error_code);
///     }
///     AsReqResponse::AsRep(as_rep) => {
///         let ticket = as_rep.ticket;
///         println!("Ticket obtained for service {}", ticket.sname);
///     }
/// }
/// ```
///
pub struct AsRequester {
    as_options: AsReqOptions,
    transporter: Box<dyn Transporter>,
    kdc_address: IpAddr,
}

impl AsRequester {
    pub fn new(realm: AsciiString, kdc_address: IpAddr) -> Self {
        return Self {
            kdc_address,
            transporter: new_transporter(kdc_address, TransportProtocol::TCP),
            as_options: AsReqOptions::new(realm),
        };
    }

    pub fn request(
        &self,
        username: &AsciiString,
        user_key: Option<&Key>,
    ) -> Result<AsReqResponse> {
        return AsRequest::request(
            username,
            user_key,
            &self.as_options,
            &self.transporter,
        );
    }

    pub fn etypes(&self) -> &HashSet<i32> {
        return self.as_options.etypes();
    }

    pub fn set_etype(&mut self, etype: i32) -> Result<()> {
        return self.as_options.set_etype(etype);
    }

    pub fn set_etypes(&mut self, etypes: HashSet<i32>) -> Result<()> {
        return self.as_options.set_etypes(etypes);
    }

    pub fn kdc_options(&self) -> u32 {
        return self.as_options.kdc_options();
    }

    pub fn realm(&self) -> &AsciiString {
        return self.as_options.realm();
    }

    pub fn set_transport_protocol(
        &mut self,
        transport_protocol: TransportProtocol,
    ) {
        self.transporter =
            new_transporter(self.kdc_address, transport_protocol);
    }

    #[cfg(test)]
    pub fn set_transporter(&mut self, transporter: Box<dyn Transporter>) {
        self.transporter = transporter;
    }
}

struct AsRequest {}

impl AsRequest {
    #[allow(clippy::borrowed_box)]
    pub fn request(
        username: &AsciiString,
        user_key: Option<&Key>,
        options: &AsReqOptions,
        transporter: &Box<dyn Transporter>,
    ) -> Result<AsReqResponse> {
        let raw_as_req =
            AsReqBuilder::build_as_req(username, user_key, options).unwrap();
        let raw_response = transporter.request_and_response(&raw_as_req)?;
        return Self::parse_as_request_response(&raw_response);
    }

    fn parse_as_request_response(raw_response: &[u8]) -> Result<AsReqResponse> {
        match KrbError::parse(raw_response) {
            Ok((_, krb_error)) => {
                return Ok(AsReqResponse::KrbError(krb_error));
            }
            Err(_) => {
                let as_rep = AsRep::parse(raw_response)?.1;
                return Ok(AsReqResponse::AsRep(as_rep));
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::net::Ipv4Addr;

    #[test]
    fn receive_krb_error() {
        struct FakeTransporter {}

        impl Transporter for FakeTransporter {
            fn request_and_response(
                &self,
                _raw_request: &[u8],
            ) -> Result<Vec<u8>> {
                return Ok(vec![
                    0x7e, 0x81, 0xdc, 0x30, 0x81, 0xd9, 0xa0, 0x03, 0x02, 0x01,
                    0x05, 0xa1, 0x03, 0x02, 0x01, 0x1e, 0xa4, 0x11, 0x18, 0x0f,
                    0x32, 0x30, 0x31, 0x39, 0x30, 0x34, 0x31, 0x38, 0x30, 0x36,
                    0x30, 0x30, 0x33, 0x31, 0x5a, 0xa5, 0x05, 0x02, 0x03, 0x05,
                    0x34, 0x2f, 0xa6, 0x03, 0x02, 0x01, 0x19, 0xa9, 0x10, 0x1b,
                    0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e, 0x48,
                    0x45, 0x41, 0x52, 0x54, 0x53, 0xaa, 0x23, 0x30, 0x21, 0xa0,
                    0x03, 0x02, 0x01, 0x02, 0xa1, 0x1a, 0x30, 0x18, 0x1b, 0x06,
                    0x6b, 0x72, 0x62, 0x74, 0x67, 0x74, 0x1b, 0x0e, 0x4b, 0x49,
                    0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e, 0x48, 0x45, 0x41, 0x52,
                    0x54, 0x53, 0xac, 0x77, 0x04, 0x75, 0x30, 0x73, 0x30, 0x50,
                    0xa1, 0x03, 0x02, 0x01, 0x13, 0xa2, 0x49, 0x04, 0x47, 0x30,
                    0x45, 0x30, 0x1d, 0xa0, 0x03, 0x02, 0x01, 0x12, 0xa1, 0x16,
                    0x1b, 0x14, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e,
                    0x48, 0x45, 0x41, 0x52, 0x54, 0x53, 0x6d, 0x69, 0x63, 0x6b,
                    0x65, 0x79, 0x30, 0x05, 0xa0, 0x03, 0x02, 0x01, 0x17, 0x30,
                    0x1d, 0xa0, 0x03, 0x02, 0x01, 0x03, 0xa1, 0x16, 0x1b, 0x14,
                    0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e, 0x48, 0x45,
                    0x41, 0x52, 0x54, 0x53, 0x6d, 0x69, 0x63, 0x6b, 0x65, 0x79,
                    0x30, 0x09, 0xa1, 0x03, 0x02, 0x01, 0x02, 0xa2, 0x02, 0x04,
                    0x00, 0x30, 0x09, 0xa1, 0x03, 0x02, 0x01, 0x10, 0xa2, 0x02,
                    0x04, 0x00, 0x30, 0x09, 0xa1, 0x03, 0x02, 0x01, 0x0f, 0xa2,
                    0x02, 0x04, 0x00,
                ]);
            }
        }

        let mut as_requester = AsRequester::new(
            AsciiString::from_ascii("KINGDOM.HEARTS").unwrap(),
            IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
        );
        as_requester.set_transporter(Box::new(FakeTransporter {}));

        match as_requester
            .request(&AsciiString::from_ascii("Mickey").unwrap(), None)
            .unwrap()
        {
            AsReqResponse::KrbError(_) => {}
            _ => {
                unreachable!();
            }
        }
    }

    #[test]
    fn receive_as_rep() {
        struct FakeTransporter {}

        impl Transporter for FakeTransporter {
            fn request_and_response(
                &self,
                _raw_request: &[u8],
            ) -> Result<Vec<u8>> {
                return Ok(vec![
                    0x6b, 0x82, 0x05, 0xe3, 0x30, 0x82, 0x05, 0xdf, 0xa0, 0x03,
                    0x02, 0x01, 0x05, 0xa1, 0x03, 0x02, 0x01, 0x0b, 0xa2, 0x2e,
                    0x30, 0x2c, 0x30, 0x2a, 0xa1, 0x03, 0x02, 0x01, 0x13, 0xa2,
                    0x23, 0x04, 0x21, 0x30, 0x1f, 0x30, 0x1d, 0xa0, 0x03, 0x02,
                    0x01, 0x12, 0xa1, 0x16, 0x1b, 0x14, 0x4b, 0x49, 0x4e, 0x47,
                    0x44, 0x4f, 0x4d, 0x2e, 0x48, 0x45, 0x41, 0x52, 0x54, 0x53,
                    0x6d, 0x69, 0x63, 0x6b, 0x65, 0x79, 0xa3, 0x10, 0x1b, 0x0e,
                    0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e, 0x48, 0x45,
                    0x41, 0x52, 0x54, 0x53, 0xa4, 0x13, 0x30, 0x11, 0xa0, 0x03,
                    0x02, 0x01, 0x01, 0xa1, 0x0a, 0x30, 0x08, 0x1b, 0x06, 0x6d,
                    0x69, 0x63, 0x6b, 0x65, 0x79, 0xa5, 0x82, 0x04, 0x17, 0x61,
                    0x82, 0x04, 0x13, 0x30, 0x82, 0x04, 0x0f, 0xa0, 0x03, 0x02,
                    0x01, 0x05, 0xa1, 0x10, 0x1b, 0x0e, 0x4b, 0x49, 0x4e, 0x47,
                    0x44, 0x4f, 0x4d, 0x2e, 0x48, 0x45, 0x41, 0x52, 0x54, 0x53,
                    0xa2, 0x23, 0x30, 0x21, 0xa0, 0x03, 0x02, 0x01, 0x02, 0xa1,
                    0x1a, 0x30, 0x18, 0x1b, 0x06, 0x6b, 0x72, 0x62, 0x74, 0x67,
                    0x74, 0x1b, 0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d,
                    0x2e, 0x48, 0x45, 0x41, 0x52, 0x54, 0x53, 0xa3, 0x82, 0x03,
                    0xcf, 0x30, 0x82, 0x03, 0xcb, 0xa0, 0x03, 0x02, 0x01, 0x12,
                    0xa1, 0x03, 0x02, 0x01, 0x02, 0xa2, 0x82, 0x03, 0xbd, 0x04,
                    0x82, 0x03, 0xb9, 0x4e, 0xc1, 0x75, 0x6d, 0x5e, 0xf6, 0x84,
                    0x18, 0x5f, 0x33, 0x21, 0x24, 0x54, 0x02, 0x40, 0x79, 0x23,
                    0x48, 0x2f, 0x39, 0xdd, 0x5a, 0xa1, 0x68, 0x79, 0x3f, 0x1a,
                    0x33, 0x0f, 0xcd, 0xe3, 0xe6, 0x3d, 0x0a, 0x3b, 0x39, 0x22,
                    0xd3, 0x6c, 0xb5, 0x89, 0xd3, 0x8f, 0xcb, 0x4a, 0xbe, 0x8f,
                    0xcb, 0xae, 0x72, 0x96, 0x7f, 0x98, 0x7c, 0x4d, 0x52, 0xbd,
                    0xa9, 0xae, 0xe6, 0xd1, 0x1c, 0x21, 0x9c, 0x2b, 0x7f, 0x2b,
                    0xd8, 0x29, 0xd6, 0x6a, 0x82, 0x5d, 0xaf, 0x0a, 0x51, 0x94,
                    0xd5, 0x00, 0xfa, 0x4d, 0xf1, 0x78, 0x88, 0x6d, 0xbf, 0x5f,
                    0x5e, 0x7c, 0x5a, 0xd0, 0xf6, 0x74, 0xac, 0x14, 0x58, 0xbf,
                    0x6c, 0xeb, 0xa1, 0x1a, 0xaa, 0x5b, 0x65, 0x4c, 0x16, 0x9c,
                    0xcc, 0xa5, 0xb1, 0x2c, 0x43, 0x1d, 0x05, 0x71, 0xa9, 0x05,
                    0xd7, 0x9e, 0x86, 0x50, 0x44, 0xeb, 0x9e, 0x33, 0x2d, 0xad,
                    0x21, 0xc9, 0x2e, 0x37, 0x67, 0x46, 0x13, 0xa5, 0x96, 0x30,
                    0xbf, 0x9e, 0xfa, 0x55, 0x80, 0x7f, 0x9b, 0x8d, 0x53, 0xe3,
                    0x08, 0xf0, 0xa9, 0xfe, 0x88, 0xd8, 0xa9, 0x16, 0xcb, 0x02,
                    0xa6, 0x63, 0x1e, 0x89, 0xa2, 0xf2, 0xe1, 0x86, 0x8f, 0x50,
                    0x89, 0x34, 0xa2, 0x9f, 0x64, 0xd6, 0xe5, 0x9c, 0x67, 0xf0,
                    0x56, 0xbb, 0x0d, 0xbb, 0xaf, 0x1d, 0xd8, 0xf3, 0xc5, 0xc7,
                    0xb9, 0xa0, 0x24, 0xeb, 0x0b, 0x87, 0x0f, 0x40, 0x7e, 0xdd,
                    0xe7, 0x88, 0xeb, 0xd2, 0x7e, 0xa3, 0x93, 0xc9, 0xc4, 0x1b,
                    0x5a, 0xf1, 0xf5, 0x54, 0x09, 0xc6, 0x38, 0x9f, 0xd2, 0x02,
                    0xaa, 0x5c, 0xf3, 0x17, 0x4e, 0x29, 0x97, 0xaf, 0xc2, 0xf2,
                    0xe3, 0x00, 0xb1, 0x49, 0x7d, 0x97, 0x3f, 0x49, 0xe3, 0xf7,
                    0x0b, 0x5b, 0x76, 0xc8, 0x89, 0x3c, 0xff, 0x27, 0x4a, 0x7a,
                    0x80, 0xe1, 0x67, 0x6e, 0xb0, 0xc2, 0x35, 0xf9, 0xaa, 0xb7,
                    0x65, 0x3e, 0x8c, 0x8d, 0x2a, 0x69, 0x9a, 0xdc, 0xeb, 0x53,
                    0x7d, 0xd9, 0xc8, 0x5d, 0xa5, 0x1c, 0x5f, 0xab, 0x52, 0xf5,
                    0x35, 0xd9, 0x76, 0x5f, 0x7b, 0x63, 0xd7, 0x35, 0x30, 0x52,
                    0x94, 0x2c, 0x37, 0x99, 0x9b, 0x5a, 0x83, 0x37, 0x5d, 0x52,
                    0x85, 0xc0, 0x8b, 0xa1, 0xac, 0xe6, 0xcc, 0x64, 0x51, 0x23,
                    0x7f, 0x21, 0x47, 0x95, 0x6d, 0xb7, 0xcb, 0x45, 0x78, 0xf4,
                    0xbf, 0xd9, 0x26, 0x3c, 0x82, 0xc5, 0x64, 0x75, 0x7a, 0x8f,
                    0x3f, 0xa1, 0x46, 0x3d, 0x4e, 0x4d, 0x11, 0xee, 0xf1, 0xae,
                    0xc4, 0x3a, 0x09, 0xa8, 0xfc, 0x89, 0x1f, 0x37, 0xe0, 0xe4,
                    0xf5, 0x44, 0x33, 0xa5, 0xec, 0xbb, 0xf5, 0x0e, 0xc0, 0x1d,
                    0x54, 0x52, 0x41, 0xc4, 0xf8, 0x65, 0xc7, 0x3d, 0x10, 0xab,
                    0x4b, 0x90, 0x28, 0xb1, 0x62, 0x85, 0x5d, 0xf1, 0xd7, 0xe0,
                    0xd2, 0x0f, 0x12, 0x51, 0x2f, 0x0d, 0xc5, 0x9f, 0xab, 0x8b,
                    0x93, 0x2f, 0x72, 0xb4, 0x74, 0xdd, 0xdd, 0x29, 0x0a, 0x6f,
                    0xa7, 0x2a, 0xc1, 0x82, 0x5e, 0xfc, 0xb2, 0x27, 0x3f, 0xa0,
                    0x7d, 0xce, 0xd2, 0x40, 0x13, 0xcb, 0x0a, 0xde, 0x0d, 0xc5,
                    0xc4, 0x45, 0x1f, 0x62, 0xfb, 0x5a, 0xd6, 0x3d, 0x91, 0x44,
                    0x85, 0x0c, 0x11, 0x76, 0x6a, 0x6f, 0x65, 0x3b, 0xc8, 0x67,
                    0x06, 0x36, 0x6d, 0x01, 0x3d, 0xdb, 0x22, 0x03, 0x75, 0xc5,
                    0xb2, 0x56, 0xf3, 0xed, 0x6c, 0x25, 0x2d, 0x7d, 0x21, 0xc1,
                    0xa5, 0xb6, 0xe6, 0x3c, 0xbd, 0xb8, 0x16, 0x0a, 0x36, 0x6e,
                    0x60, 0x9c, 0xd6, 0x23, 0x53, 0x2b, 0xbc, 0x14, 0xbe, 0xfd,
                    0x1b, 0x57, 0xbb, 0x0b, 0xfd, 0x7e, 0x65, 0xe3, 0xc7, 0x00,
                    0x56, 0x6a, 0x9f, 0xf4, 0xf3, 0x83, 0xae, 0x2f, 0x4c, 0xe6,
                    0x68, 0x80, 0x8d, 0x55, 0x0f, 0xfa, 0x87, 0xbf, 0xcc, 0x62,
                    0xe4, 0xa8, 0x37, 0xe2, 0x04, 0x1f, 0xc3, 0x4b, 0x39, 0xb2,
                    0x70, 0x88, 0x2e, 0x4c, 0x89, 0xfb, 0x3d, 0x74, 0xae, 0x82,
                    0xf8, 0xea, 0x9c, 0x7d, 0xf1, 0x78, 0x22, 0xac, 0x2f, 0x96,
                    0x52, 0x13, 0x1b, 0x8b, 0xcc, 0x01, 0x17, 0x9d, 0xff, 0x4f,
                    0x1f, 0xeb, 0x3d, 0x97, 0xea, 0x2a, 0x0c, 0xd6, 0x0c, 0x5c,
                    0x7a, 0x41, 0x1f, 0x6e, 0x5b, 0x9b, 0x5d, 0x16, 0xb8, 0x0c,
                    0x08, 0x93, 0x51, 0xa4, 0xb9, 0x4a, 0xe9, 0x4c, 0x3a, 0x60,
                    0x88, 0x74, 0xf0, 0xa8, 0xb5, 0x2a, 0x9f, 0x34, 0x6f, 0xad,
                    0x8a, 0xed, 0xc2, 0x9e, 0x38, 0xdc, 0x74, 0x33, 0x62, 0x6b,
                    0x4e, 0x1d, 0x82, 0x92, 0xa8, 0xd2, 0xda, 0x86, 0x9d, 0x90,
                    0xcb, 0x6b, 0x19, 0x07, 0x56, 0xa3, 0x59, 0x10, 0x57, 0x89,
                    0xd1, 0x00, 0xcc, 0x94, 0x7c, 0xcd, 0x0c, 0xdc, 0x74, 0xfb,
                    0x5f, 0xe4, 0x6f, 0x73, 0x1e, 0xa8, 0x8e, 0xad, 0x31, 0x0d,
                    0x07, 0xe7, 0x8d, 0x23, 0xf9, 0x8f, 0xed, 0x04, 0x2b, 0x47,
                    0x3f, 0x54, 0xcb, 0xbb, 0x0b, 0xf8, 0xc6, 0x32, 0xd5, 0x7d,
                    0x20, 0x92, 0xfd, 0xa6, 0xba, 0x75, 0x02, 0x42, 0x5a, 0x72,
                    0xa4, 0xdf, 0xd0, 0x0a, 0xb0, 0x33, 0x80, 0xf1, 0xea, 0x15,
                    0x3d, 0x5f, 0xae, 0xcf, 0x1f, 0xcc, 0x44, 0xb5, 0x5f, 0x69,
                    0x9f, 0x90, 0x40, 0xf0, 0x6e, 0xc9, 0x9a, 0x63, 0x52, 0x97,
                    0x1e, 0xed, 0xc8, 0x05, 0x12, 0xb2, 0xfb, 0xad, 0xe1, 0x13,
                    0xa5, 0x39, 0x53, 0x88, 0xaf, 0xcf, 0xbe, 0x01, 0x4a, 0x65,
                    0x62, 0xf0, 0x35, 0x2f, 0x76, 0x9a, 0x8b, 0xc3, 0xbc, 0x43,
                    0x5b, 0xc4, 0x91, 0xcc, 0x04, 0xfe, 0xcc, 0xc4, 0xf5, 0xa3,
                    0x27, 0x88, 0x97, 0x49, 0xca, 0xe2, 0x33, 0x1d, 0xff, 0x96,
                    0x33, 0x4b, 0x50, 0x49, 0x86, 0xdc, 0x65, 0x9f, 0x55, 0xc1,
                    0xb6, 0x85, 0xe5, 0x9f, 0x3d, 0xd1, 0x87, 0x84, 0xd8, 0x08,
                    0x9f, 0x03, 0x4c, 0xc7, 0xa8, 0x8b, 0x59, 0xb7, 0x58, 0xd2,
                    0x10, 0x1c, 0x3f, 0xf9, 0x2d, 0x5f, 0x37, 0x5c, 0x70, 0x90,
                    0x84, 0xea, 0x4b, 0x37, 0x55, 0x9c, 0x12, 0x2d, 0xa4, 0xb2,
                    0x75, 0x5d, 0x37, 0xfc, 0x7c, 0xa7, 0x19, 0xb4, 0x88, 0xba,
                    0xf3, 0xea, 0xe2, 0xf1, 0xa2, 0xe3, 0x23, 0xd6, 0x5e, 0x6e,
                    0xf8, 0x37, 0x61, 0xf2, 0xec, 0xd8, 0x17, 0x19, 0xa3, 0x69,
                    0xbd, 0xd8, 0x51, 0x17, 0x37, 0xa3, 0xc6, 0x8f, 0x26, 0xf1,
                    0x19, 0x6f, 0xf4, 0xf9, 0xdb, 0x09, 0xef, 0x70, 0x88, 0x81,
                    0x78, 0xfd, 0x2e, 0x60, 0xdb, 0xdf, 0x6e, 0xe9, 0xf6, 0xef,
                    0xb0, 0x7e, 0x75, 0xc5, 0x18, 0x39, 0xdc, 0x4b, 0x33, 0xda,
                    0x51, 0xad, 0xe4, 0x7b, 0x7d, 0x46, 0xd2, 0x39, 0x62, 0xf1,
                    0x71, 0x4c, 0xda, 0x49, 0xa0, 0x7b, 0xc7, 0x67, 0xe8, 0x47,
                    0x6e, 0x3a, 0x43, 0x4e, 0x31, 0x0e, 0x30, 0x3b, 0x60, 0x7d,
                    0xc1, 0x0c, 0x4e, 0x82, 0x7e, 0xf6, 0x02, 0xcf, 0xd4, 0xfe,
                    0x8f, 0x39, 0x8e, 0xce, 0xe6, 0x7b, 0x3a, 0xc7, 0xae, 0xde,
                    0xf1, 0x2b, 0xae, 0x4e, 0xd8, 0x60, 0x7e, 0x8a, 0x10, 0xdf,
                    0xdf, 0xb8, 0x57, 0x5b, 0x7c, 0xb3, 0x80, 0x55, 0x16, 0x4c,
                    0xab, 0x62, 0x39, 0xb7, 0xa4, 0x4c, 0xd3, 0xaa, 0xca, 0x5b,
                    0xd1, 0xb5, 0xcb, 0xf4, 0x46, 0xfc, 0xa6, 0x82, 0x01, 0x5f,
                    0x30, 0x82, 0x01, 0x5b, 0xa0, 0x03, 0x02, 0x01, 0x12, 0xa1,
                    0x03, 0x02, 0x01, 0x02, 0xa2, 0x82, 0x01, 0x4d, 0x04, 0x82,
                    0x01, 0x49, 0xe2, 0xbb, 0xa9, 0x28, 0x8e, 0x2e, 0x2e, 0x3e,
                    0xf5, 0xfa, 0xee, 0x6d, 0x9e, 0xde, 0x0e, 0x77, 0x38, 0x70,
                    0x9b, 0xca, 0xc4, 0x74, 0x6f, 0x7f, 0x00, 0xbf, 0xc7, 0x92,
                    0x30, 0x30, 0x98, 0xd5, 0x29, 0x76, 0x49, 0xab, 0x92, 0x31,
                    0x7f, 0x7b, 0xbe, 0x49, 0x4b, 0x37, 0xe7, 0xf9, 0x33, 0x0f,
                    0x14, 0x88, 0x8e, 0x4c, 0xda, 0xb8, 0x80, 0xfb, 0x84, 0xde,
                    0x97, 0xd9, 0x02, 0xb7, 0x44, 0x4d, 0x66, 0x73, 0x5a, 0x62,
                    0xcf, 0x47, 0xc4, 0x42, 0x69, 0xba, 0xdb, 0x64, 0x8b, 0x61,
                    0x61, 0x71, 0xeb, 0xc1, 0xf6, 0x10, 0x01, 0x26, 0x65, 0xa0,
                    0xab, 0x8d, 0x30, 0xad, 0xa9, 0x13, 0x30, 0xda, 0x74, 0x6a,
                    0xd7, 0x00, 0xa7, 0x24, 0x16, 0x1d, 0x99, 0xe0, 0x7c, 0xb9,
                    0x77, 0x98, 0x3e, 0x04, 0x3d, 0xa7, 0x21, 0x6b, 0xee, 0xec,
                    0x1a, 0xb1, 0x68, 0xb9, 0x93, 0xf9, 0x06, 0xdb, 0xce, 0x2e,
                    0x51, 0x77, 0x56, 0xd7, 0x8f, 0xe1, 0x36, 0xc8, 0x6a, 0xca,
                    0xb1, 0x3d, 0x71, 0xdf, 0x8d, 0x0c, 0x83, 0x68, 0x9b, 0x9b,
                    0xe8, 0xc9, 0xe7, 0x0f, 0xf3, 0x5e, 0xd2, 0xc6, 0x8c, 0xad,
                    0xf0, 0x93, 0x4e, 0xe8, 0xac, 0x9a, 0xe5, 0x84, 0x25, 0x5d,
                    0xde, 0x5f, 0xb9, 0x48, 0xbe, 0xd5, 0x93, 0xc7, 0x53, 0xd7,
                    0xe8, 0x86, 0xd4, 0xc5, 0x5a, 0xfd, 0xab, 0xe0, 0x5d, 0x75,
                    0x87, 0x8b, 0x5b, 0x06, 0x09, 0x4d, 0xd7, 0x0a, 0x35, 0x91,
                    0xee, 0x68, 0x8b, 0x91, 0x34, 0x38, 0x43, 0x75, 0x9a, 0xaf,
                    0x20, 0xf7, 0x32, 0x61, 0xe6, 0xea, 0xcb, 0x8d, 0x7c, 0x34,
                    0x55, 0x8a, 0x08, 0x26, 0x96, 0x79, 0xff, 0xbd, 0x74, 0x0c,
                    0x8a, 0x7c, 0xb2, 0xfb, 0x06, 0x90, 0xc3, 0xf5, 0x77, 0xba,
                    0x3a, 0x53, 0x0c, 0x6f, 0x41, 0x4d, 0x35, 0xe8, 0x0c, 0x75,
                    0x4e, 0x14, 0x90, 0xdc, 0xf1, 0xa7, 0x70, 0x5f, 0xe1, 0x90,
                    0xa4, 0x54, 0xdc, 0x5f, 0xb8, 0x18, 0x41, 0x5f, 0xfc, 0xc1,
                    0xe6, 0x5f, 0xf9, 0x54, 0x77, 0xf5, 0x5c, 0x7b, 0x31, 0xf0,
                    0xd2, 0xcf, 0x05, 0x35, 0x12, 0xea, 0xdb, 0xfc, 0x80, 0x71,
                    0xf8, 0xcc, 0x4a, 0x2d, 0x3b, 0x54, 0xf2, 0xde, 0xe2, 0x20,
                    0x32, 0x7e, 0xf1, 0xa7, 0x14, 0x25, 0x1b, 0x88, 0x38, 0x0e,
                    0x24, 0x46, 0x04, 0x09, 0x87, 0xf9, 0xd6, 0xe1, 0xce, 0x3b,
                    0xe8, 0x42, 0x95, 0xb7, 0x6c, 0x75, 0xc0, 0x7d, 0x13, 0xa0,
                    0x7b,
                ]);
            }
        }

        let mut as_requester = AsRequester::new(
            AsciiString::from_ascii("KINGDOM.HEARTS").unwrap(),
            IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
        );
        as_requester.set_transporter(Box::new(FakeTransporter {}));

        match as_requester
            .request(
                &AsciiString::from_ascii("mickey").unwrap(),
                Some(&Key::Secret("Minnie1234".to_string())),
            )
            .unwrap()
        {
            AsReqResponse::KrbError(_) => {
                unreachable!();
            }
            _ => {}
        }
    }
}
